package com.ruoyi.web.task;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.domain.BhPlans;
import com.ruoyi.web.mapper.BhPlansMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class SchedulerTask {
    @Autowired
    private BhPlansMapper bhPlansMapper;

    @Scheduled(cron = "0 1 0 * * ? ")
    public void updateDangerStatus() {
        // 获取隐患列表
        List<BhPlans> dangerList = bhPlansMapper.selectList(new LambdaQueryWrapper<BhPlans>().eq(BhPlans::getDelFlag, 0).eq(BhPlans::getRiskStatus, "1").gt(BhPlans::getHiddenDangerNum, 0));
        if (CollUtil.isNotEmpty(dangerList)) {
            for (BhPlans danger : dangerList) {
                Date rectifyDeadline = DateUtils.parseDate(DateUtils.parseDateToStr("yyyy-MM-dd", danger.getRectifyDeadline()));
                Date now = new Date();
                if (rectifyDeadline.compareTo(now) <= 0) {
                    // 状态变成已逾期
                    BhPlans modParam = new BhPlans();
                    modParam.setId(danger.getId());
                    modParam.setRiskStatus("4");
                    bhPlansMapper.updateById(modParam);
                }
            }
        }
    }
}
